package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.entity.ChannelEntity;
import com.lkd.entity.VendingMachineEntity;
import com.lkd.service.ChannelService;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-08-20 14:37
 */
@Component
@Topic(TopicConfig.VMS_SUPPLY_TOPIC)
@Slf4j
public class SupplyTaskCompleteHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;
    @Autowired
    private ChannelService channelService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(String jsonMsg) throws IOException {
        SupplyContract contract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
        System.out.println(contract);

        String innerCode = contract.getInnerCode();
        //1.校验参数
        if (StringUtils.isBlank(innerCode)
                || CollectionUtils.isEmpty(contract.getSupplyData())
        ) {
            log.error("补货工单消息处理失败..." + jsonMsg);
            return;
        }

        /**
         * 1.更新售货机最后补货时间
         *
         * 2.按照售货机编号查询货道列表
         *
         * 3.更新货道库存和最后补货时间
         */
        LocalDateTime now = LocalDateTime.now();
        boolean update = vendingMachineService.update(Wrappers.<VendingMachineEntity>lambdaUpdate()
                .eq(VendingMachineEntity::getInnerCode, innerCode)
                .set(VendingMachineEntity::getLastSupplyTime, now)
        );

        if (!update) {
            //更新失败
            log.error("售货机不存在,编号:" + innerCode);
            return;
        }


        List<SupplyChannel> supplyData = contract.getSupplyData();
        //update xxx set capacity = capacity + 2
        for (SupplyChannel supplyDatum : supplyData) {
            channelService.update(Wrappers.<ChannelEntity>lambdaUpdate()
                    .eq(ChannelEntity::getInnerCode,innerCode)
                    .eq(ChannelEntity::getChannelCode,supplyDatum.getChannelId())
                    .setSql("current_capacity = current_capacity + " + supplyDatum.getCapacity())
                    .set(ChannelEntity::getLastSupplyTime,now)
                    .set(ChannelEntity::getSkuId,supplyDatum.getSkuId())
            );
        }

//        List<String> channelCodes = supplyData.stream().map(SupplyChannel::getChannelId).collect(Collectors.toList());
//
//        List<ChannelEntity> list = channelService.list(Wrappers.<ChannelEntity>lambdaQuery().eq(ChannelEntity::getInnerCode, innerCode)
//                .in(ChannelEntity::getChannelCode, channelCodes)
//        );
//
//        if (CollectionUtils.isNotEmpty(list)) {
//            for (ChannelEntity channelEntity : list) {
//                for (SupplyChannel supplyDatum : supplyData) {
//                    if (channelEntity.getChannelCode().equals(supplyDatum.getChannelId())) {
//
//                        channelEntity.setLastSupplyTime(now);
//                        channelEntity.setCurrentCapacity(Math.min(channelEntity.getCurrentCapacity()
//                                + supplyDatum.getCapacity(), channelEntity.getMaxCapacity()));
//
//                        channelEntity.setSkuId(supplyDatum.getSkuId());
//                        break;
//                    }
//                }
//            }
//
//            channelService.saveBatch(list);
//        }


    }

}
